﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataControl;
using System.Data;
using System.Text;

namespace ATW.MT.BUS
{
    public class bus_T_Exception
    {
        DataAccess da = null;
        public bus_T_Exception()
        {
            da = new DataAccess();
        }
        #region 方法
        /// <summary>
        /// 查询异常信息列表
        /// </summary>
        /// <param name="contractid"></param>
        /// <returns></returns>
        public DataTable ExceptionQuery(string contractid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select TE.Exid,TE.Content,
								TE.create_time,
								TE.isread,
                                TC.[type],
								case when TC.[type]=1 then 'admin'
								     when TC.[type]=2 then Ures.tauName end nameFrom,
								case when TC.[type]=1 then Ureq.tauName
								     when TC.[type]=2 then 'admin'end nameTo 
                             from T_Exception TE
                            left join T_Contract TC on TE.ContractID=TC.ContractID
                            left join T_Request req on TC.RequestID=req.RequestID
                            left join T_AppUser Ureq on req.tauloginid=Ureq.tauLoginId
                            left join T_Response res on TC.ResponseID=res.ResponseID
                            left join T_AppUser Ures on res.tauloginid=Ures.tauLoginId
                            where TC.ContractID='{0}' order by TE.create_time desc", contractid);
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 新增异常
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="content"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public int addException(string cid, string content, string type)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into T_Exception (contractID,type,content,isread) values('{0}','{1}','{2}','0')",
                cid, type, content);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 阅读异常信息
        /// </summary>
        /// <param name="exid"></param>
        /// <returns></returns>
        public int readException(string exid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update T_Exception set isread='1' where Exid='{0}'", exid);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 获取推送用户
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public string getContractUser(string cid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select req.tauloginid from T_Exception te 
                    left join T_Contract tc on te.ContractID=tc.ContractID
                    left join T_Request req on req.RequestID=tc.RequestID
                    where tc.ContractID='{0}'", cid);
            object o = da.ExecuteScalar(sql.ToString());
            if (o != null && o.ToString() != "")
            {
                return o.ToString();
            }
            return "";
        }
        public string getRequestID(string contractID)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select RequestID from T_Contract where ContractID='{0}'", contractID);
            object o = da.ExecuteScalar(sql.ToString());
            if (o != null && o.ToString() != "")
            {
                return o.ToString();
            }
            return "";
        }
        #endregion
    }
}